Optional Transformations.f

Requires Vectors
Requires OpenGL
Requires Tools
Requires FixedPoint
Requires Dorothy.f

Only Forth Definitions

Type: transform
   vector.field pos
   vector.field rotation
   vector.field scale
   rgba32.field color
End-type

\ Transformations

: transform.gltransform3d   ( transform -- )
  @+ p>f @+ p>f @+ p>f glTranslatef
  @+ p>f @+ p>f @+ p>f
    0e fdup 1e glRotatef
    0e 1e fover glRotatef
    1e 0e fdup glRotatef
  @+ p>f @+ p>f @ p>f glScalef ;

: transform.gltransform2d   ( transform -- )
  @+ p>f @+ p>f @+ p>f glTranslatef
  cell+ cell+ @+ p>f 0e fdup 1e glRotatef
  @+ p>f @+ p>f @ p>f glScalef ;

method: transform.2transform (  .x .y -- .x .y )
   roll.@ 2rotate
   scale.2@ 2mul
   pos.2@ 2+ ;

method: transform.2transform-aligned (  .x .y -- .x .y )
   scale.2@ 2mul
   pos.2@ 2+ ;

{
: vertices-transform2d   ( addr count transform -- )
   locals| t |
   a@ >r
   over a!
   0 do
      h@+ swap h@+ swap -rot t 2transform swap h!a+ h!a+
      12 + 12 +a
   loop
   r> a! ;
}

{
-? 0 value t
: vertices-fast-transform2d   ( addr count transform -- )
   to t
   0 do
   dup h@ t ->x @ + over h!
   dup 2 + h@ t ->y @ + over 2 + h!
   16 +
   loop drop ;
{
   dup ->x 2v@ locals| y x t c a |
   c 4 / 0 do
      a h@ x + a h!
      a 2 + h@ y + a 2 + h!
      16 +to a
   loop ;
}
